/*
 * @Author: Hongkun Luo
 * @Date: 2024-05-16 08:14:54
 * @LastEditors: Hongkun Luo
 * @Description:
 *
 * Hongkun Luo
 */
#include <chrono>
#include <iostream>
#include "omp.h"

#include <opencv2/opencv.hpp>
#include <onnxruntime_cxx_api.h>
#include "XFeat.h"

int main()
{
	std::string xfeatModelPath = "../model/xfeat_dualscale.onnx";
	std::string matchingModelPath = "../model/matching.onnx";
	cv::Mat image0 = cv::imread("../assets/1403636579763555584.png", cv::IMREAD_COLOR);
	cv::Mat image1 = cv::imread("../assets/1403636579813555456.png", cv::IMREAD_COLOR);
	cv::Mat mkpts0, feats0, sc0;
	cv::Mat mkpts1, feats1, sc1;
	cv::Mat matches, batch_indexes;

	// Init xfeat object
	XFeat xfeat(xfeatModelPath, matchingModelPath);
	// Extract feats
	xfeat.detectAndCompute(image0, mkpts0, feats0, sc0);
	xfeat.detectAndCompute(image1, mkpts1, feats1, sc1);
	// Matching and refine
	xfeat.matchStar(mkpts0, feats0, sc0, mkpts1, feats1, matches, batch_indexes);

	// Print results
	std::cout << "matches: " << matches.rows << "x" << matches.cols << "x" << matches.channels() << std::endl;
	std::cout << "batch_indexes: " << batch_indexes.rows << "x" << batch_indexes.cols << "x" << batch_indexes.channels() << std::endl;

	// Get points
	std::vector<cv::Point2f> points0, points1;
	for (int i = 0; i < matches.rows; i++)
	{
		points0.push_back(cv::Point2f(*((float *)matches.data + i * 4), *((float *)matches.data + i * 4 + 1)));
		points1.push_back(cv::Point2f(*((float *)matches.data + i * 4 + 2), *((float *)matches.data + i * 4 + 3)));
	}
	// Visualization
	cv::Mat drawImage;
	drawImage =XFeat::warpCornersAndDrawMatches(points0, points1, image0, image1);

	cv::imshow("demo", drawImage);
	cv::waitKey();

	return 0;
}